In this lesson, I'm going to make a health pickup.
In the last lesson, we had the simple pickup that if we touch it, it disappears.
That's cool and all, but not very helpful for our game.
We want something like a health pickup.
And before I get started with my game playing, I'm going to go to the workspace because I need to show
you something.
I'm going to open a workspace.
Here's my character.
If I select it, you can see that it's highlighted in the world.
If I open it, I'll look for something called the humanoid.
The humanoid allows me to run around and jump.
It's a special Roblox object.
It also controls health.
So if humanoid selected, let's go down and look for something that says health.
Here's health.
We have health, which is 100 and max health.
Remember those two?
So if you have health of ten changes of ten, you're going to watch.
It's going to show some sort of damage here.
Bam, not much, but it's down to ten points max.
Health is 100, so we're at 10%.
What happens if I go to zero?
I died anyway.
So this is what we're going to work with.
We're going to work with the health and the max health.
Let's go ahead and look at our guy, our little player again.
Go to humanoid.
Notice that the humanoid is one level down from the character, right?
That is called a first child.
The next level is not the grandchild.
Right?
That's the second level.
The second child.
So remember that term, first child.
All these are first children.
They're right underneath the character.
You got to know that for what we're going to do today.
So let's go ahead and turn this off.
Go to our workspace, pick up.
There's the pickup.
I'm going to change this to health.
Pick up.
Here we go.
And I'll change the script name.
Oops.
I selected it, but that's okay.
We're going to use it and I'll change that to let's just call it give health.
Here we go.
Now, we already got a variable for the pickup and we have this touch function.
The other part is what's going to be touching my pickup, which is going to be like a hand or a foot.
But it could be other things too, like a ball or a tree or whatever.
We're not really checking for that, but we need to have a print statement here to see what touched
it.
We saw that last video, so we'll delete that.
And what I want to do is I want to get a part and check to see if it's associated with the humanoid
that I know is like a character, some sort of character with a humanoid in it.
I'm going to make a variable local cube and humans are going to be short for humanoid.
I'm going to look for a humanoid.
I'm going to get the other part, which might be a hand or a foot or a head with a dot parent.
That takes me up to the character level.
The hands parent is the character.
I'm going to try and find first child that's a humanoid.
Remember I said that that was the first child that was underneath the character.
If we find the humanoid, this is going to have value.
It's going to be the humanoid.
If we don't find the value, this is going to be nil.
Just be careful.
If you spell this wrong, you won't find it, but you won't get an error.
It's just not going to work and you're going to have trouble.
That's a little bit hard.
Hard to deal with, but you'll get better at this as it goes along.
Let's say if human does not equal nil, then we found a humanoid.
We'll put the end here.
So we've got a humanoid.
We have access to the health.
HUME Dot health.
Let's make it equal to full health.
Remember what that was?
It was 100.
But this is called a literal, right?
You're putting the actual number in your script.
Not really that great of a practice.
We could do something like this.
HUME Dot max health.
And that's going to give us full health if the humanoid exists or whoever touches it.
And they have a humanoid, so a zombie could touch it and get that, too.
What happens if we don't find the humanoid?
Well, it will be nil.
This will be false.
So the humanoid.
Not equally nil will be a false statement, and then we'll just skip it.
Let's see what we got.
Let's put let's put a zombie in there to hurt us, though.
We want to check to see if we get full health.
Let's say drooling zombie.
This is a Roblox product, so theoretically.
Good.
Cool.
Yeah.
And it warns you that there are scripts.
I trust these scripts, though.
But you should be careful because people can put bad stuff in the scripts.
Let's hit the play.
Now.
We don't want to hit.
We don't want to hit that health right away or we're going to use it up and we're not going to get any
benefits from it.
So we want to get hurt.
We got a little bit of hurt.
We're down we're down to the yellow.
We're going to touch this boom.
It's gone.
So the health bar, when you're 100% healthy, disappears, that's pretty cool.
Let's go ahead and make sure that we can't touch it when we're full of health, though.
So go to our health strip.
How do we do that?
Well, check to see if the humanoid exists.
And if it does, then we'll say if.
HUME Dot health is less than.
HUME Dot Mac's health.
Then and I'm going to format this.
So now we have two checks.
We check to see if the humanoid exists.
Right.
Make sure it's not nil.
If it does exist, we want to make sure that the health is, in fact, less than max health.
Then we will award health, destroy the script.
Anything that goes wrong here, we're going to skip right out of that function.
Nothing's going to happen.
So go ahead and play it.
We should be able to run through this thing and not destroy it.
Yeah, because we're full of health, right?
Then when we get hurt.
Cool beans.
We are 100% health.
All right.
One more thing I want to go over in our give health.
So we have this embedded if statement, it's inside another statement.
Well, we know a little bit about if statements and we have these logical operators where we can chain
conditions together.
So we don't need to if statements, but it's perfectly fine to have to.
We could do this and and then get this.
Put it here and then let's get rid of this.
If statement, don't forget the end, then we'll format it.
There we go.
Get rid of those blank spaces.
A blank lines.
Now, what happens if this is nil?
Right.
Well, this we have a problem, right?
Because we're trying to access health on a nil object.
Well, obviously doesn't have health if it's nil.
Well, we don't have to worry, because what's going to happen is we're going to check this.
If it is not nil, then we're going to go and we're going to check this whole condition.
Right.
And it will chunk these the logical operator is lower priority than this comparison right here.
But if you're worried about that, you can always use parentheses to make sure that you're grouping
your grouping your statements.
Well, cool.
So another thing that we can do, roadblocks is tricky like this.
This is supposed to be true or false, but if something exists and there's no comparison, it will be
considered true.
If it does not exist, it's nil.
In other words, it's nil.
It will be considered false.
So we can actually say, Hey, if HUME and that's checking to see if it exists, then we can do this
comparison.
Pretty cool.
Let's see if it works.
All right.
We can run through our little ball there.
Let's check our output window.
That's looking good.
Make this a little bigger.
Get hurt.
Bomber or yellow?
Cool beans.
We are full of health.
So that is a simple health pickup and we will just keep building on this until you get your own gain.